package com.chen.music.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.music.model.Comment;
import com.chen.music.service.CommentService;
import com.chen.music.util.CommonResult;
import com.chen.music.util.MessageConst;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author boat
 * @since 2022-01-13
 */
@RestController
@RequestMapping("/comment")
@Api(tags = "评论管理")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @GetMapping("/song/{id}")
    @ApiOperation("根据id获取歌曲评论")
    public CommonResult findBySongId(@PathVariable("id") Integer id) {
        try {
            LambdaQueryWrapper<Comment> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(id != null, Comment::getSongId, id);
            return new CommonResult(200, true, commentService.list(wrapper));
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(500, false, MessageConst.INTERFACE_EXCEPTION);
        }
    }

    @GetMapping("/songList/{id}")
    @ApiOperation("根据id获取歌曲评论")
    public CommonResult findBySongListId(@PathVariable("id") Integer id) {
        try {
            LambdaQueryWrapper<Comment> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(id != null, Comment::getSongListId, id);
            return new CommonResult(200, true, commentService.list(wrapper));
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(500, false, MessageConst.INTERFACE_EXCEPTION);
        }
    }

    @GetMapping("/{currentPage}/{pageSize}")
    @ApiOperation("检索用户评论")
    public CommonResult findById(@PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize, Comment comment) {
        try {
            LambdaQueryWrapper<Comment> wrapper = Wrappers.lambdaQuery();
            wrapper.like(!StringUtils.isEmpty(comment.getContent()), Comment::getContent, comment.getContent());
            Page<Comment> page = commentService.page(new Page<>(currentPage, pageSize), wrapper);
            if (currentPage > page.getPages()) {
                return new CommonResult(200, true, commentService.page(new Page<>(page.getPages(), pageSize), wrapper));
            }
            return new CommonResult(200, true, commentService.page(new Page<>(currentPage, pageSize), wrapper));
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(500, false, MessageConst.INTERFACE_EXCEPTION);
        }
    }

    @PutMapping("/like")
    @ApiOperation("点赞评论")
    public CommonResult addLike(Comment comment) {
        try {
            if (commentService.updateById(comment)) {
                return new CommonResult(200, true, MessageConst.LIKE_SUCCESS);
            }
            return new CommonResult(200, false, MessageConst.LIKE_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(500, false, MessageConst.INTERFACE_EXCEPTION);
        }
    }

    @PostMapping("/add")
    @ApiOperation("新增评论")
    public CommonResult add(Comment comment) {
        try {
            if (StringUtils.isEmpty(comment)) {
                return new CommonResult(200, false, MessageConst.ADD_COMMENT_FAIL);
            }
            if (commentService.save(comment)) {
                return new CommonResult(200, true, MessageConst.ADD_COMMENT_SUCCESS);
            }
            return new CommonResult(200, false, MessageConst.ADD_COMMENT_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(500, false, MessageConst.INTERFACE_EXCEPTION);
        }
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除评论")
    public CommonResult add(@PathVariable("id") Integer id) {
        try {
            if (id != null && commentService.removeById(id)) {
                return new CommonResult(200, true, MessageConst.DELETE_COMMENT_SUCCESS);
            }
            return new CommonResult(200, false, MessageConst.DELETE_COMMENT_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(500, false, MessageConst.INTERFACE_EXCEPTION);
        }
    }
}

